package com.tiza.xiaoyoubao.ui.fragment;

import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.tianze.library.base.BaseFragment;
import com.tianze.library.utils.AppUtils;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.net.ApiHttpClient;
import com.tiza.xiaoyoubao.net.JsonCallback;
import com.tiza.xiaoyoubao.net.ResponseInfo;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Request;

/**
 * Created by mapple on 17/2/27.
 */

public class ForgetPassWordFragment extends BaseFragment {

    //用户名
    @BindView(R.id.editTextPhone)
    EditText editTextPhone;
    //完成
    @BindView(R.id.buttonPaw)
    Button buttonPaw;
    //验证码
    @BindView(R.id.editTextVerify)
    EditText editTextVerify;
    //验证按钮
    @BindView(R.id.buttonVerify)
    Button buttonVerify;
    //密码
    @BindView(R.id.editTextPasswd)
    EditText editTextPasswd;
    //密码
    @BindView(R.id.editTextConfirmPasswd)
    EditText editTextConfirmPasswd;
    private String userName;
    private String verifyCode = "";
    private String passWord;
    private String userVerify;

    private static final int SECONDS = 60;

    private int nowSeconds = SECONDS;

    private static Handler sHandler = new Handler();

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (nowSeconds >= 2) {
                nowSeconds--;
                buttonVerify.setText(nowSeconds + "s后重发");
                sHandler.postDelayed(this, 1000);
            } else {
                nowSeconds = SECONDS;
                buttonVerify.setText("获取验证码");
                buttonVerify.setEnabled(true);
            }
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_forget_password;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

    }


    @OnClick({R.id.buttonVerify, R.id.buttonPaw})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.buttonVerify:
                userName = editTextPhone.getText().toString();
                if (AppUtils.isMobile(userName)) {
                    doVerify();
                } else {
                    showWarnDialog("请输入正确的手机号");
                }
                break;
            case R.id.buttonPaw:
                String message = checkPhone();
                if (TextUtils.isEmpty(message)) {
                    changePassWord();
                } else {
                    showWarnDialog(message);
                }
                break;
            default:
                break;
        }
    }

    private String checkPhone() {
        userName = editTextPhone.getText().toString().trim();
        if (!AppUtils.isMobile(userName)) return "请输入正确的手机号";
        userVerify = editTextVerify.getText().toString().trim();
        if (TextUtils.isEmpty(userVerify)) return "请输入验证码";
        if (!verifyCode.equals(userVerify) || TextUtils.isEmpty(verifyCode)) return "验证码有误";
        passWord = editTextPasswd.getText().toString().trim();
        if (passWord.length() == 0) return "请输入密码";
        String passWord2 = editTextConfirmPasswd.getText().toString().trim();
        if (passWord2.length() == 0) return "请输入验证密码";
        if (!passWord.equals(passWord2)) return "两次输入密码不一致";
        return "";
    }

    private void doVerify() {
        buttonVerify.setEnabled(false);
        sHandler.post(runnable);
        ApiHttpClient.verify(ForgetPassWordFragment.this, userName,
                             new JsonCallback<String>(String.class, true) {

                                 @Override
                                 public void onBefore(Request request) {
                                     super.onBefore(request);
                                 }

                                 @Override
                                 public void onError(Call call, Exception e) {
                                     toast("验证码获取失败");
                                     sHandler.removeCallbacks(runnable);
                                     nowSeconds = SECONDS;
                                     buttonVerify.setText("获取验证码");
                                     buttonVerify.setEnabled(true);
                                 }

                                 @Override
                                 public void onResponse(ResponseInfo<String> response) {
                                     int code = response.getCode();
                                     String message = response.getMessage();
                                     if (code == 1) {
                                         showWarnDialog(message);
                                         sHandler.removeCallbacks(runnable);
                                         nowSeconds = SECONDS;
                                         buttonVerify.setText("获取验证码");
                                         buttonVerify.setEnabled(true);
                                         return;
                                     }
                                     Map<String, Object> _map = response.getFirstEntity();
                                     verifyCode = String.valueOf(_map.get("verify"));
                                 }

                                 @Override
                                 public void onAfter() {
                                     super.onAfter();
                                 }
                             }
        );
    }

    private void changePassWord() {
        buttonVerify.setEnabled(false);
        ApiHttpClient.setUserInfo(ForgetPassWordFragment.this, userName, passWord,"1",
                                     new JsonCallback<String>(String.class, true) {

                                         @Override
                                         public void onBefore(Request request) {
                                             super.onBefore(request);
                                         }

                                         @Override
                                         public void onError(Call call, Exception e) {
                                             showWaitingDialog();
                                         }

                                         @Override
                                         public void onResponse(ResponseInfo<String> response) {
                                             int code = response.getCode();
                                             String message = response.getMessage();
                                             if (code == 1) {
                                                 showWarnDialog(message);
                                                 return;
                                             }
                                             sHandler.removeCallbacks(runnable);
                                             toast("修改密码成功!");
                                             finish();
                                         }

                                         @Override
                                         public void onAfter() {
                                             super.onAfter();
                                         }
                                     }
        );
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        sHandler.removeCallbacks(runnable);
    }
}
